# 中间件
import re

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect,render

class UserMW(MiddlewareMixin):
    def process_request(self, request): # 拦截
        path = request.path_info
        if path == '/myApp/login/' or path == '/myApp/registry/' or re.search('^/admin.*', path):       # 如果是登录或注册页面，允许通过
            return None
        else:
            if not request.session.get('username'):     # 如果没有用户名，则重定向到登录页面
                return redirect('login')
        return None
    def process_view(self, request, callback, callback_args, callback_kwargs):
        return None

    def process_response(self, request, response):
        return response

